package com.singfan.app.controller;

import com.qiniu.util.Auth;
import com.singfan.app.controller.model.request.SystemAppleCheckLoginRequest;
import com.singfan.app.controller.model.request.SystemJPushRegistrationRequest;
import com.singfan.app.controller.model.request.SystemSevenbullTokenRequest;
import com.singfan.app.controller.model.response.*;
import com.singfan.app.util.AppleUtil;
import com.singfan.common.util.StringUtil;
import com.singfan.core.component.BarberTokenHolder;
import com.singfan.core.component.CustomerTokenHolder;
import com.singfan.core.dao.database.model.BarberToken;
import com.singfan.core.dao.database.model.Customer;
import com.singfan.core.dao.database.model.CustomerToken;
import com.singfan.core.dao.database.model.JpushInfo;
import com.singfan.core.dao.database.model.constant.PlatformType;
import com.singfan.core.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author: hansonghe
 * Date: 2015-09-08 16:13
 * Desc:
 */
@Controller
public class SystemController extends SingFanControllerBase {
    @Autowired
    ISystemService systemService;

    @RequestMapping(value="/v1/system/apple/check/status", method= RequestMethod.POST)
    public void appleCheckStatus(HttpServletRequest request, HttpServletResponse response) {
        SystemAppleCheckStatusResponse result = new SystemAppleCheckStatusResponse();
        String ip = getIpAddress(request);

        result.setResultCode(ResultCode.SUCCESS);
        result.setPassed(AppleUtil.isPassed());

        traceLog.debug(String.format("SystemController.appleCheckStatus() successful -> resultCode:%1$d, ip:%2$s, isPassed:%3$b", ResultCode.SUCCESS, ip, AppleUtil.isPassed()));

        bean2Response(response, result);
    }


    @RequestMapping(value="/v1/system/apple/check/login", method= RequestMethod.POST)
    public void appleCheckLogin(HttpServletRequest request, HttpServletResponse response) {
        SystemAppleCheckLoginResponse result = new SystemAppleCheckLoginResponse();
        SystemAppleCheckLoginRequest checkLoginRequest = request2Bean(request, SystemAppleCheckLoginRequest.class);
        String ip = getIpAddress(request);

        String phoneNumber = checkLoginRequest.getPhoneNumber();
        String pwd = checkLoginRequest.getPwd();
        if ("13304315677".equals(phoneNumber) && "123456".equals(pwd)){
            Customer customer = customerService.createOrUpdateCustomer(phoneNumber, "iPhone8,1", 10);
            result.setResultCode(ResultCode.SUCCESS);
            String token = customerService.createAccessToken(customer.getId(), customer.getCityId());
            result.setAccessToken(token);
            result.setAvatar(customer.getAvatar());
            result.setPhoneNumber(customer.getPhoneNumber());

            traceLog.debug(String.format("SystemController.appleCheckLogin() successful -> resultCode:%1$d, ip:%2$s, phoneNumber:%3$s, pwd:%4$s", ResultCode.SUCCESS, ip, phoneNumber, pwd));
        }
        else {
            result.setResultCode(ResultCode.USER_PWD_ERROR);

            traceLog.error(String.format("SystemController.appleCheckLogin() error with USER_PWD_ERROR -> resultCode:%1$d, ip:%2$s, phoneNumber:%3$s, pwd:%4$s", ResultCode.USER_PWD_ERROR, ip, phoneNumber, pwd));
        }

        bean2Response(response, result);
    }

    @RequestMapping(value="/v1/system/jpush/registration", method= RequestMethod.POST)
    public void jpushRegistration(HttpServletRequest request, HttpServletResponse response) {
        SystemJPushRegistrationResponse result = new SystemJPushRegistrationResponse();
        SystemJPushRegistrationRequest registrationRequest = request2Bean(request, SystemJPushRegistrationRequest.class);

        String ip = getIpAddress(request);
        String accessToken = registrationRequest.getAccessToken();
        int platformType = registrationRequest.getPlatformType();
        String registrationId = registrationRequest.getRegistrationId();

        CustomerToken customerToken = CustomerTokenHolder.getCurrentCustomerToken();
        BarberToken barberToken = BarberTokenHolder.getCurrentBarberToken();

        boolean checkResult = true;
        if (!PlatformType.isPlatformType(platformType)){
            checkResult = false;
        }
        else if (customerToken == null && barberToken == null){
            checkResult = false;
        }
        else if (StringUtil.isNullOrEmpty(registrationId)){
            checkResult = false;
        }

        if (checkResult){
            JpushInfo jpushInfo = new JpushInfo();
            jpushInfo.setPlatformType(platformType);
            jpushInfo.setRegistrationCode(registrationId);
            if (customerToken != null){
                jpushInfo.setUserType(1);
                jpushInfo.setUserId(customerToken.getCustomerId());
            }
            else {
                jpushInfo.setUserType(2);
                jpushInfo.setUserId(barberToken.getBarberId());
            }

            systemService.insertOrUpdate(jpushInfo);

            result.setResultCode(ResultCode.SUCCESS);
            traceLog.debug(String.format("SystemController.jpushRegistration() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, platformType:%4$d, registrationId:%5$s", ResultCode.SUCCESS, ip, accessToken, platformType, registrationId));
        }
        else {
            result.setResultCode(ResultCode.REQUEST_PARAM_ERROR);
            traceLog.error(String.format("SystemController.jpushRegistration() error with REQUEST_PARAM_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, platformType:%4$d, registrationId:%5$s", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, platformType, registrationId));
        }

        bean2Response(response, result);
    }

    @RequestMapping(value="/v1/system/sevenbull/updatetoken", method= RequestMethod.POST)
    public void sevenbullUpdatetoken(HttpServletRequest request, HttpServletResponse response) {
        SystemSevenbullTokenResponse result = new SystemSevenbullTokenResponse();
        SystemSevenbullTokenRequest registrationRequest = request2Bean(request, SystemSevenbullTokenRequest.class);

        String ip = getIpAddress(request);
        String accessToken = registrationRequest.getAccessToken();

        CustomerToken customerToken = CustomerTokenHolder.getCurrentCustomerToken();
        BarberToken barberToken = BarberTokenHolder.getCurrentBarberToken();

        if (customerToken != null || barberToken != null){
            Auth auth = Auth.create("dpLrsXOAeQtki8qdgpC8ZiaDL3W3H3ZRLU2vkaV1", "zXsG2to1cP1gnlkUllJ6MP9965d1ecegGKZSm8sr");
            String uploadToken = auth.uploadToken("singfan-picture");
            result.setUpdateToken(uploadToken);
            result.setResultCode(ResultCode.SUCCESS);
            traceLog.error(String.format("SystemController.sevenbullUpdatetoken() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, uploadToken:%4$s", ResultCode.SUCCESS, ip, accessToken, uploadToken));
        }
        else {
            result.setResultCode(ResultCode.ACCESS_TOKEN_ERROR);
            traceLog.error(String.format("SystemController.sevenbullUpdatetoken() error with ACCESS_TOKEN_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken));
        }

        bean2Response(response, result);
    }
}
